package com.msw.design.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 动态代理：
 *
 * Created by minshiwu on 2017/6/4.
 */
public class DynamicSubject implements InvocationHandler {

    /**
     * 通过组合的设计模式：将真实的Subject传入
     */
    private Object sub;


    public DynamicSubject(Object sub) {
        this.sub = sub;
    }

    /**
     *
     *
     * @param proxy
     * @param method
     * @param args
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        preHandle();
        String res = (String) method.invoke(sub, args);
        postHandle();
        return res;
    }


    public void preHandle() {
        System.out.println("before invoke method .............");
    }


    public void postHandle() {
        System.out.println("after invoke method ..............");
    }


}
